/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode.interview.linklist;

import com.xiaohujr.xuexue.leetcode.tree.ListNode;

/**
 * * https://leetcode-cn.com/problems/swap-nodes-in-pairs/
 *
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2021/1/14
 */
public class SwapPairs {
    /**
     * 递归的作用在于让其他节点也两两交换，然后返回两两交换的第一个节点。
     * @param head
     * @return
     */
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = head.next;
        head.next = swapPairs(newHead.next);
        newHead.next = head;
        return newHead;
    }

    public static void main(String[] args) {
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);
        ListNode d = new ListNode(4);
        a.next = b;
        b.next = c;
        c.next = d;
        new SwapPairs().swapPairs(a);
    }
}
